package com.mars.manager.user.provider.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mars.common.exception.BizException;
import com.mars.common.utils.RandomUitl;
import com.mars.common.utils.security.PasswordUtils;
import com.mars.common.utils.security.SecurityUtils;
import com.mars.component.redis.JedisClient;
import com.mars.manager.user.entity.User;
import com.mars.manager.user.exception.UserBizException;
import com.mars.manager.user.provider.mapper.UserMapper;
import com.mars.manager.user.service.UserService;
import com.mars.service.support.BaseService;

@Service
public class UserServiceImpl extends BaseService<User, Long> implements UserService {
	
	@Autowired
	private UserMapper mapper ;
	
	@Autowired
	private JedisClient jedisClient ;

	@Override
	public int insertSelective(User entity) throws UserBizException {
		if(null != super.checkValue("username", entity.getUsername())) {
			throw new UserBizException(UserBizException.USERNAME_EXITS, entity.getUsername()) ;
		}
		if(null != super.checkValue("email", entity.getEmail())) {
			throw new UserBizException(UserBizException.EMAIL_EXITS, entity.getEmail()) ;
		}
		if(null != super.checkValue("mobile", entity.getMobile())) {
			throw new UserBizException(UserBizException.MOBILE_EXITS, entity.getMobile()) ;
		}
		
		//密码加密加盐
		entity.setPassword(PasswordUtils.encodePassword(entity.getUsername(), entity.getPassword(), RandomUitl.uuid()));
		
		return super.insertSelective(entity);
	}
	
	@Override
	public int updateSelective(User entity) throws BizException {
		User oldUser = super.selectById(entity.getId()) ;
		if(null != oldUser.getUsername() && !oldUser.getUsername().equals(entity.getUsername())) {
			if(null != super.checkValue("username", entity.getUsername())) {
				throw new UserBizException(UserBizException.USERNAME_EXITS, entity.getUsername()) ;
			}
		}
		if(null != oldUser.getEmail() && !oldUser.getEmail().equals(entity.getEmail())) {
			if(null != super.checkValue("email", entity.getEmail())) {
				throw new UserBizException(UserBizException.EMAIL_EXITS, entity.getEmail()) ;
			}
		}
		if(null != oldUser.getMobile() && !oldUser.getMobile().equals(entity.getMobile())) {
			if(null != super.checkValue("mobile", entity.getMobile())) {
				throw new UserBizException(UserBizException.MOBILE_EXITS, entity.getMobile()) ;
			}
		}
		
		return super.updateSelective(entity);
	}
	
	@Override
	public User selectById(Long id) throws BizException {
		return super.selectById(id) ;
	}

	@Override
	public User gerUserByUserName(String username) {
		User entity = new User() ;
		entity.setUsername(username); 
		return super.selectOne(entity) ;
	}

	@Override
	public Boolean checkData(String content, Integer type) {
		if(type == 1) {// 用户名是否存在
			User checkValue = this.mapper.checkValue("username", content) ;
			return checkValue != null ? true : false ;
		} else if(type == 2) {// 邮件地址是否存在
			User checkValue = this.mapper.checkValue("email", content) ;
			return checkValue != null ? true : false ;
		} else if(type == 3) {// 手机号码是否存在
			User checkValue = this.mapper.checkValue("mobile", content) ;
			return checkValue != null ? true : false ;
		}
		return null;
	}

	@Override
	public Integer updatePassword(Long id, String username, String password) {
		if(null == username) {
			throw new UserBizException(UserBizException.NOT_NULL_USERNAME) ;
		}
		if(null == password) {
			throw new UserBizException(UserBizException.NOT_NULL_PASSWORD) ;
		}
		String uuid = RandomUitl.uuid() ;
		return this.mapper.updatePassword(id, SecurityUtils.sha256(username+password, uuid), uuid);
	}

	@Override
	public Integer updateAccountStatus(Long id, boolean status) {
		return this.mapper.updateAccountStatus(id, status);
	}
}
